<?php
namespace Admin\Controller;
use Think\Controller;
/**
* 系统消息控制器
*/
class MessageController extends Controller{
	/**
     * 系统给用户发通知消息
     * @access public
     * @param int $user_id 用户id
     * @param string $content 内容
     * @param int $id 活动id
     */
	public function addSysMsg($user_id,$content,$id=""){
		$now = time();
		if($id){
			$msg = array(
				'fa_id'		=> 0,
				'shou_id'	=> $user_id,
				'content'	=> $content, 
				'add_time'	=> $now,
				'status'	=> 0,
				'interval'  =>  intval(time()/(5*60)),
				'type'		=> 0,
				'url'  		=> 'http://'.$_SERVER['SERVER_NAME'].'/index.php/Home/Index/activity.html?cate_id='.$id
			);
		}else{
			$msg = array(
				'fa_id'		=> 0,
				'shou_id'	=> $user_id,
				'content'	=> $content,
				'add_time'	=> $now,
				'status'	=> 0,
				'interval'  =>  intval(time()/(5*60)),
				'type'		=> 0,
				'url'  		=> ''
			);
		}
		$res = M('message')->add($msg);
		$this->messageSystemList($user_id,$content,$now);
		return $res ? $res : false;
	}

	/**
     * 系统给用户发通知消息，通知他支付诚意金成功
     * @access public
     * @param string $user_id 用户id
     * @param int $id 活动id
     */
	public function sendPayChengyiMessage($user_id){
		$now = time();
		$content = "您退出被邀约活动或爽约，账户已被冻结，须缴纳诚意金解冻用户";
		$msg = array(
			'fa_id'		=> 0,
			'shou_id'	=> $user_id,
			'content'	=> $content,
			'add_time'	=> $now,
			'interval'  =>  intval(time()/(5*60)),
			'type'		=> 0,
			'url'  		=> "0",
		);
		M('message')->add($msg);
		$this->messageSystemList($user_id,$content,$now);
	}
	/**
     * 添加聊天信息组
     * @access public
     * @param string $user_id 用户id
     * @param string $content 聊天内容
     * @param int $now 时间
     */
	public function messageSystemList($user_id,$content,$now){

		$mess1 = M('message_list')
			->where("(fa_id=0 AND shou_id=$user_id)")
			->find();
		if (!$mess1){
			$mess1['id']=M('message_list')->add(array('fa_id'=>0,'shou_id'=>$user_id,'news'=>$content,'add_time'=>$now));
		}
		else{
			M('message_list')->where('id='.$mess1['id'])->save(array('news'=>$content,'add_time'=>$now));
		}
		system_to_one($user_id,$content,$now);
	}
}